import RE from './request.js'
export function KEY() {
  return 'EPDBZ-GWJCU-2V2V6-2KSSM-B6PFO-AQBAN'
}

const LocationFail = function() {
  //位置获取失败


}


export function open() {
  uni.showModal({
    title: '无法获取位置信息',
    content: '请允许我们获取您的位置信息',
    confirmText: '去开启',
    showCancel: false,
    success() {
      uni.openSetting({
        success(res1) {
          console.log(res1)
          if (res1.authSetting['scope.userLocation']) {
            //用户已经授权，但是没有获取成功
            uni.getLocation({
              success(res2) {
                uni.setStorageSync('location', res2)
                parseLocation(res2).then(re => {
                  console.log(re)
                }).catch(re => {
                  console.log(re)
                })
              },
              fail() {
                uni.showToast({
                  icon: 'none',
                  title: '位置获取失败'
                })
                LocationFail();

              }
            })
          } else {
            //用户还是没有授权，继续打开设置
            open();
          }
        }
      })
    }
  })
}

export function geLo() {
  uni.showModal({
    title: '位置获取失败',
    content: '位置获取失败，可能是您的微信没有定位权限，请重新授予',
    confirmText: '重新定位',
    showCancel: false,
    success() {
      uni.getLocation({
        success(res) {
          uni.setStorageSync('location', res)
          parseLocation(re).catch(res => {
            console.log(re)
          })
        },
        fail() {
          getLo().then(re => {
            console.log(re)
          }).catch(re => {
            console.log(re)
          })
        }
      })
    }
  })
}
export function parseLocation(location) {
  console.log("联网获取")
  return new Promise(function(resolve, reject) {
    RE.request('https://apis.map.qq.com/ws/geocoder/v1/', {
      key: KEY(),
      location: location.latitude + "," + location.longitude,
    }).then(res => {
      if (res.status == 0) {
        let _obj = res.result
        let obj = {
          detail: _obj.address,
          adcode: _obj.ad_info.adcode,
          citycode: _obj.ad_info.adcode.toString().substring(0, 4) + "00",
          lat: location.latitude,
          lon: location.longitude
        }
        uni.setStorageSync('city', obj)
        resolve(obj)
      } else {
        uni.showToast({
          icon: 'none',
          title: '地埋位置解析错误'
        })
        reject()
      }
    }).catch(re => {
      console.log(re)
    })
  })
}
export function getLocation() {
  return new Promise(function(resolve, reject) {
    //获取用户地理位置
    uni.getLocation({
      success(res) {
        // console.log("第一次获取 成功")
        // console.log(res)
        uni.setStorageSync('location', res)
		resolve(res);
        // parseLocation(res).then(re => {
        //   resolve(re);
        // }).catch(re => {
        //   console.log(re)
        // })
      },
      fail(res) {
        console.log(res)
        uni.hideLoading()
        //没有获取到位置信息
        uni.getSetting({
          success(res) {
            if (res.authSetting['scope.userLocation']) {
              uni.getLocation({
                success(res2) {
                  uni.setStorageSync('location', res2)
                  parseLocation(res2).then(re => {
                    resolve(re);
                  })
                },
                faile() {
                  geLo();
                  reject();
                }
              })
              //用户已经授权，但是没有获取成功
            } else {
              console.log("用户没有授权")
              //用户拒绝授权，打开设置
              open()
              reject();
            }
          }
        })
      }
    })
  })
}
